package cn.hinglo.gameclaw.client.action;

import cn.hinglo.gameclaw.client.service.InvitationService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @createTime: 2018/1/18
 * @author: HingLo
 * @description: 邀请码管理相关的
 */
@RestController
@RequestMapping("/client/i/")
public class InvitationAction extends BaseController {

    private final InvitationService invitationService;

    @Autowired
    public InvitationAction(InvitationService invitationService) {
        this.invitationService = invitationService;
    }

    /**
     * 添加邀请记录
     *
     * @param code 邀请码
     * @return 填写邀请码是否成功
     */
    @PostMapping("add")
    public Result addInvitation(@RequestParam("code") String code) {
        String userId = getUserId(request);
        return invitationService.addInvitationService(userId, code);
    }

    /**
     * 查看自己的邀请码被那些人填写了
     *
     * @return 记录列表
     */
    @GetMapping("list")
    public Result list() {
        return success(invitationService.listService(getUserId(request)));
    }

    /***
     * 统计该人的邀请码得到的钻石总量 与邀请以为好友可以得到的钻石数量
     * @return 钻石总量
     */
    @GetMapping("count")
    public Result count() {
        Map<String, Integer> map = new HashMap<>(3);
        map.put("friend_gold", InvitationService.FRIEND_GOLD);
        map.put("gold", InvitationService.GOLD);
        //如果你的邀请码没有人填写，就默认为0
        Integer total = invitationService.countService(getUserId(request));
        map.put("total", total == null ? 0 : total);
        return success(map);
    }
}
